package IC.SymbolTables.Types;

/**
 * 	Probably needed because of the concept of function overloading....
 *         +
 *               It will be easier to check typing (no need to search in the tree again for type checking)  
 */
public class MethodType extends Type
{
	private InstanceType[] _paramTypes;
	private ReturnType _returnType;
	
	public InstanceType[] GetParamTypes() { return _paramTypes; }
	public ReturnType GetReturnType() { return _returnType; }
	
	public MethodType(ReturnType returnType, InstanceType[] paramTypes)
	{
		_paramTypes = (paramTypes == null) ? new InstanceType[0] : paramTypes;
		_returnType = returnType;
	}
	
	public boolean IsInstanceOf(Type type)
	{
		if (!(type instanceof MethodType) || _paramTypes.length != ((MethodType)type)._paramTypes.length)
			return false;
		
		int index = 0;
		
		for (Type[] ParamsArray = ((MethodType)type)._paramTypes; index < _paramTypes.length; index++)
			if (!_paramTypes[index].equals(ParamsArray[index]))
				return false;
		
		return _returnType.equals(((MethodType)type)._returnType);
	}
	
	public String toString()
	{
		return String.format("{%s -> %s}", ParamsToString(), _returnType.toString());
	}
	
	private String ParamsToString()
	{
		String ret = "";
		for (InstanceType type : _paramTypes)
			ret += type.toString() + ", ";
		
		if (ret.length() != 0)
			return ret.substring(0, ret.length() - 2);
		
		return "";
	}
}
